<?php 
$MYT = $GLOBALS['MYT'];

$MDDC = 'RD';
$BWDC = 'ED';

if (!isset($OBJCTCL)) {
	$OBJCTCL = $_POST['OBJCTCL'];
}
if ($OBJCTCL == 0) {
	$MDDC = 'ED';
	$BWDC = 'RD';
}
if(!isset($_POST['DC_ViewMode'])) {
	$_POST['DC_ViewMode'] = 0;
}
$_Stat = 2;
$CheckRows = 'bgcolor="#DADADA" class="Selector"
		onMouseOver="this.className=\'Selector_Over\'"
		onMouseOut="this.className=\'Selector\'"
		onClick="this.className=\'Selector_Click\';';

mn_field('hidden','ED','DC_ViewMode',$_POST['DC_ViewMode'],'','','','');
if ($_POST['DC_ViewMode'] == 1) {
	$_Stat = '2,3,4';
}
?>
<div id="ACDC"></div>
<div id="NWDC"></div>
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<?php
if($BWDC=='ED'){
	echo '<tr bgcolor="#BACCD1">
		<td colspan="8">Documentos';
	echo mn_field('button',$BWDC,'FMDC','Novo Documento','100','38','False','SUBMIT"
			onClick="replaceUrl(\'NWDC\',\'_response/call.php?get=Operative/frm_senderdocument&OBJCTCL='.$OBJCTCL.'\');');
	if ($_POST['DC_ViewMode'] == 0) {
		mn_field('button',$BWDC,'FMDC','Ver Documentos Caducados','','','False','SUBMIT"
			onClick="document.ClientProperties.DC_ViewMode.value=1;
			document.ClientProperties.action=\'_response/call.php?get=Operative/view/viewsenderdocument\';
			subform(\'ClientProperties\',\'MNDC\');');
	} else {
		mn_field('button',$BWDC,'FMDC','Esconder Documentos Caducados','','','False','SUBMIT"
			onClick="document.ClientProperties.DC_ViewMode.value=0;
			document.ClientProperties.action=\'_response/call.php?get=Operative/view/viewsenderdocument\';
			subform(\'ClientProperties\',\'MNDC\');');
	}
}?>
	</td>
	</tr>
	<tr bgcolor="#E6E6E6">
		<td></td>
		<td>Tipo</td>
		<td width="15%">Número</td>
		<td width="15%">Data de Emissão</td>
		<td width="15%">Data de Validade</td>
		<td>Orgão Expedidor</td>
		<td>Funcionário</td>
		<td>Arquivo</td>
	</tr>
	<?php
	$Docs = $MYT->db_array("SELECT dtd_ID,
	  				   tdc_Tipo,
					   tdc_Id,
					   dtd_Numero,
					   DATE(dtd_FechaEmision) AS dtd_FechaEmision,
					   DATE(dtd_FechaCaducidad) AS dtd_FechaCaducidad,
					   dtd_Emisor,
					   tdc_Validacion,
					   dtd_Status,
					   IF(DC_OFFICE>0,office_number,Us_Origen) AS dtd_Arquivo,
					   main_users.name AS dtd_Func
				FROM hd_documentos
				INNER JOIN hd_documentostipos ON tdc_Id = dtd_Tipo
				LEFT OUTER JOIN systemoffice.main_users ON main_users.ref = dtd_Usuario
				LEFT OUTER JOIN systemoffice.main_offices ON main_offices.ref = DC_OFFICE
				LEFT OUTER JOIN hd_usuarios ON Us_Numero = dtd_Usuario
				WHERE dtd_Cliente = {$OBJCTCL}
				AND dtd_Cliente > 0 AND dtd_Status IN ({$_Stat})");

	$Sdtd_Tipo = "SELECT dtc_Id AS ID, tdc_Tipo AS NAME
					FROM hd_documentostipos
					ORDER BY NAME ASC";
	for ($dc = 1; $dc <= $Docs[0]; $dc++) {
	 $graph = 'edit.gif';
	 if ($Docs[$dc]['dtd_Status'] == 2) {
	 	if ((strtotime($Docs[$dc]['dtd_FechaCaducidad']) > 1 && strtotime($Docs[$dc]['dtd_FechaCaducidad']) < strtotime(date("Y-m-d"))) && $Docs[$dc]['tdc_Validacion'] == 1) {
				$Docs[$dc]['dtd_Status'] = 4;
				$MYT->db_query("UPDATE hd_documentos
				SET dtd_Status = 4
				WHERE dtd_ID = '{$Docs[$dc]['dtd_ID']}'");
				echo '<script charset="utf-8" type="text/javascript">alert(\'Cliente tem novos documentos caducados.\');</script>';
				$graph = 'default/gfx0094.png';
	 	}
	 } else {
			$graph = 'default/gfx0094.png';
	 }
	 echo '<tr bgcolor="#E6E6E6" '.$CheckRows.'">
        <td height="25" onClick="replaceUrl(\'NWDC\',\'_response/call.php?get=Operative/frm_senderdocument&CID='.$OBJCTCL.'&OBJCTDC='.$Docs[$dc]['dtd_ID'].'\');"><img src="_response/bin.php?get='.$graph.'"></td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; mn_field('select',$MDDC,'dtd_Tipo',$Docs[$dc]['tdc_Id'],$Sdtd_Tipo,$Docs[$dc]['tdc_Tipo'],'False',''); echo'</td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; mn_field('text',$MDDC,'dtd_Numero',$Docs[$dc]['dtd_Numero'],'30','35','False',''); echo'</td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; mn_field('text',$MDDC,'dtd_FechaEmision',$Docs[$dc]['dtd_FechaEmision'],'10','8','False',''); echo'</td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; if ($Docs[$dc]['tdc_Validacion'] == 1) {mn_field('text',$MDDC,'dtd_FechaCaducidad',$Docs[$dc]['dtd_FechaCaducidad'],'10','8','False','');} echo'</td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; mn_field('text',$MDDC,'dtd_Emisor',$Docs[$dc]['dtd_Emisor'],'35','30','False',''); echo'</td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; mn_field('text',$MDDC,'dtd_Func',$Docs[$dc]['dtd_Func'],'35','30','False',''); echo'</td>
		<td onClick="replaceUrl(\'DCDT\',\'_response/call.php?get=Operative/view/viewsenderdocumentattch&DID='.$Docs[$dc]['dtd_ID'].'\');">'; echo $Docs[$dc]['dtd_Arquivo']; echo'</td>
	</tr>';
	} ?>
</table>
<div id="DCDT"></div>
